package com.hibase.core.rule.utils;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.codec.digest.DigestUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author EDZ
 * @version 1.0
 * @date 2020/10/30 15:43
 */
public class RuleUtils {

    public static String getKey(String tableName, String versionName, String findData, String hash) {
        StringBuilder sb = new StringBuilder();
        sb.append(tableName)
                .append(".")
                .append(versionName)
//                .append(".")
//                .append(findData)
                .append(".")
                .append(hash);
        return sb.toString();
    }

    public static String getHash(String tableName, String versionName, String findData) {
        StringBuilder sb = new StringBuilder();
        sb.append(tableName)
                .append(".")
                .append(versionName)
                .append(".")
                .append(findData);
        return DigestUtils.sha1Hex(sb.toString());
    }

    public static Map stringToMap(String json) {
        HashMap<String, String> map = new HashMap<>();
        JSONObject jsonObject = JSONUtil.parseObj(json);
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            map.put(entry.getKey(), entry.getValue() + "");
        }
        return map;
    }

    public static String getType(Object o) {
        return o.getClass().toString();
    }

    /**
     * String 转int
     * @param str
     * @return
     */
    public static Integer stringToInteger(String str) {
        return Integer.parseInt(str);
    }

    /**
     * 相乘
     * @param a
     * @param b
     * @return
     */
    public static String multiply(String a, String b) {
        return new BigDecimal(a).multiply(new BigDecimal(b)).toString();
    }

    /**
     * Obj 转 map
     * @param obj
     * @return
     * @throws Exception
     */
    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }

        Map<String, Object> map = new HashMap<String, Object>();

        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }

        return map;

    }

    /**
     * 正则
     * @param regex
     * @param content
     * @return
     */
    public static Boolean regExp(String regex,String content){
        return ReUtil.contains(regex,content);
    }

    /**
     * 大写
     * @param str
     * @return
     */
    public static String upperCase(String str){
        if(StrUtil.isNotBlank(str)){
            return str.toUpperCase();
        }
        return str;
    }

    public static String lowerCase(String str){
        if(StrUtil.isNotBlank(str)){
            return str.toLowerCase();
        }
        return str;
    }
}
